package com.gz.furnitures.utils;


import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtilsByDruid {
    private static DataSource ds;
    private static ThreadLocal<Connection>threadLocalConn =new ThreadLocal<>();

    static {//初始化
        Properties properties = new Properties();
        try {
            properties.load(JdbcUtilsByDruid.class.getClassLoader().
                    getResourceAsStream("druid.properties"));
            ds= DruidDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection()  {
        Connection connection = threadLocalConn.get();
        if (connection == null) { //说明当前的threadLocalConn没有连接
            try {
                connection = ds.getConnection();
                //将连接设置成手动提交，不要自动提交
                //回滚也回滚到这里，即开始状态
                connection.setAutoCommit(false);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            //就从数据库连接池中取一个连接放入，threadLocalConn
            threadLocalConn.set(connection);
        }
        return connection;
    }

    /**
     * 重写 提交
     */
    public static void commit() {
        Connection connection = threadLocalConn.get();
        if (connection != null) {
            try {
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close(); //将连接放回连接池
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        //!!!
        //当提交后如果connection不从threadLocalConn
        //会造成threadLocalConn长时间持有该连接，造成未知影响
        threadLocalConn.remove();
    }

    /**
     * 回滚/撤销connection关联的修改、删除、添加等操作
     */
    public static void rollback() {
        Connection connection = threadLocalConn.get();
        if (connection != null) {
            try {
                connection.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        threadLocalConn.remove();
    }

    //关闭连接
    //注意:数据库连接池中close并不是真的关闭连接
    //而是把使用的Connection放回连接池
    public static void close( ResultSet rs, Statement stmt, Connection conn) {
        try {
            if(rs!=null) {
                rs.close();
            }
            if(stmt!=null) {
                stmt.close();
            }
            if(conn!=null) {
                conn.close();//与原先jdbc原本的关闭方法不同，druid这里是放回连接池
                            //涉及多态，这里的Connectcion是ds里面的Connection,调用的close方法是druid中的close方法
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
